package dpatterns.observer.weatherorama.observers.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import dpatterns.observer.weatherorama.observers.DisplayElement;
import dpatterns.observer.weatherorama.subjects.impl.WeatherData;

public class Statistics implements Observer, DisplayElement {
	private Observable observable;
	private float humidity; 
	private float temperature;
	private float pressure;
	private List<WeatherStat> stats; 
	
	public Statistics(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
		stats = new ArrayList<WeatherStat>();
	}
	
	@Override
	public void update(Observable obs, Object arg) {
		if(obs instanceof WeatherData) {
			WeatherData weatherData = (WeatherData) obs;
			humidity = weatherData.getHumidity(); 
			temperature = weatherData.getTemperature();
			pressure = weatherData.getPressure();
			stats.add(new WeatherStat(humidity, temperature, pressure));
		}
	}
	
	public void display() {
		float avgTemp = 0;
		float maxTemp = 0;
		float minTemp = 1000;
		
		for(int i = 0; i < stats.size(); i++) {
			avgTemp += stats.get(i).getTemperature();
			if(i == (stats.size()-1)) {
				avgTemp /= stats.size(); 
			}
			
			if(stats.get(i).getTemperature() > maxTemp) {
				maxTemp = stats.get(i).getTemperature();
			}
			
			if(stats.get(i).getTemperature() < minTemp) {
				minTemp = stats.get(i).getTemperature();
			}
		} 
			
		System.out.println("Avg/Max/Min temperature = "+avgTemp+"F / "+maxTemp+"F / "+minTemp+"F");
	}
	
	class WeatherStat {
		private Date date;
		private float humidity; 
		private float temperature;
		private float pressure;
		
		public WeatherStat(float humidity, float temperature, float pressure) {
			this.date = new Date();
			this.humidity = humidity;
			this.temperature = temperature;
			this.pressure = pressure;
		}

		public Date getDate() {
			return date;
		}

		public void setDate(Date date) {
			this.date = date;
		}

		public float getHumidity() {
			return humidity;
		}

		public void setHumidity(float humidity) {
			this.humidity = humidity;
		}

		public float getTemperature() {
			return temperature;
		}

		public void setTemperature(float temperature) {
			this.temperature = temperature;
		}

		public float getPressure() {
			return pressure;
		}

		public void setPressure(float pressure) {
			this.pressure = pressure;
		}
	}
}